<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>迭代器 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day07/03.生成器.html" />
    
    
    <link rel="prev" href="../day07/01.迭代.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="7.2"
        data-chapter-title="迭代器"
        data-filepath="day07/02.迭代器.md"
        data-basepath=".."
        data-revision="Sat Sep 08 2018 13:52:27 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day01/index.html">
            
                
                    <a href="../day01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Linux系统基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day01/01.操作系统.html">
            
                
                    <a href="../day01/01.操作系统.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        操作系统(科普章节)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day01/02.操作系统发展史.html">
            
                
                    <a href="../day01/02.操作系统发展史.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        操作系统发展史(科普章节)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day01/03.Ubuntu图形界面入门.html">
            
                
                    <a href="../day01/03.Ubuntu图形界面入门.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Ubuntu图形界面入门
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day01/04.文件目录.html">
            
                
                    <a href="../day01/04.文件目录.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        文件目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="day01/05.常用Linux命令的基本使用.html">
            
                
                    <a href="../day01/05.常用Linux命令的基本使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        常用Linux命令的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="day01/06.Linux终端命令格式.html">
            
                
                    <a href="../day01/06.Linux终端命令格式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        Linux终端命令格式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="day01/07.常用Linux命令选项的使用.html">
            
                
                    <a href="../day01/07.常用Linux命令选项的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        常用Linux命令选项的使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day02/index.html">
            
                
                    <a href="../day02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Linux系统命令
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day02/01.高级Linux命令及命令选项.html">
            
                
                    <a href="../day02/01.高级Linux命令及命令选项.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        高级Linux命令及命令选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day02/02.远程登录和远程拷贝.html">
            
                
                    <a href="../day02/02.远程登录和远程拷贝.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        远程登录和远程拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day02/03.软件安装与卸载.html">
            
                
                    <a href="../day02/03.软件安装与卸载.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        软件安装与卸载
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day02/04.编辑器vim.html">
            
                
                    <a href="../day02/04.编辑器vim.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        编辑器vim
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day02/05.命令总结.html">
            
                
                    <a href="../day02/05.命令总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        命令总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day03/index.html">
            
                
                    <a href="../day03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        网络-UDP
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day03/01.网络概述.html">
            
                
                    <a href="../day03/01.网络概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        网络概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day03/02.ip地址.html">
            
                
                    <a href="../day03/02.ip地址.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        IP地址
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day03/03.Linux命令-ping和ifconfig.html">
            
                
                    <a href="../day03/03.Linux命令-ping和ifconfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Linux命令-ping和ifconfig
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="day03/04.端口.html">
            
                
                    <a href="../day03/04.端口.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        端口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="day03/05.网络传输方式udp.html">
            
                
                    <a href="../day03/05.网络传输方式udp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        网络传输方式-UDP
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="day03/06.socket介绍.html">
            
                
                    <a href="../day03/06.socket介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        socket介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="day03/07.udp网络程序-发送、接收数据.html">
            
                
                    <a href="../day03/07.udp网络程序-发送、接收数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        udp网络程序-发送、接收数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="day03/08.数据编码和解码.html">
            
                
                    <a href="../day03/08.数据编码和解码.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        数据编码和解码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="day03/09.udp绑定端口号.html">
            
                
                    <a href="../day03/09.udp绑定端口号.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        udp绑定端口号
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="day03/11.案例-UDP发送广播消息.html">
            
                
                    <a href="../day03/11.案例-UDP发送广播消息.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        案例-udp发送广播消息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="day03/12.udp聊天器.html">
            
                
                    <a href="../day03/12.udp聊天器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                        案例-udp聊天器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day04/index.html">
            
                
                    <a href="../day04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        网络-TCP
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day04/01.tcp简介.html">
            
                
                    <a href="../day04/01.tcp简介.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        网络传输方式-TCP
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day04/02.tcp网络程序-客户端.html">
            
                
                    <a href="../day04/02.tcp网络程序-客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        TCP网络程序-客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day04/03.tcp网络程序-服务端.html">
            
                
                    <a href="../day04/03.tcp网络程序-服务端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        TCP网络程序-服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day04/04.tcp注意点.html">
            
                
                    <a href="../day04/04.tcp注意点.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        TCP注意点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day04/05.案例-文件下载器.html">
            
                
                    <a href="../day04/05.案例-文件下载器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        案例-文件下载器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="day04/06.tcp的3次握手.html">
            
                
                    <a href="../day04/06.tcp的3次握手.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        tcp的3次握手
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="day04/07.tcp的4次挥手.html">
            
                
                    <a href="../day04/07.tcp的4次挥手.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        tcp的4次挥手
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="day04/08.wireshark的使用.html">
            
                
                    <a href="../day04/08.wireshark的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        wireshark的使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day05/index.html">
            
                
                    <a href="../day05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        多任务-线程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day05/01.多任务的介绍.html">
            
                
                    <a href="../day05/01.多任务的介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        多任务的介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day05/02.线程.html">
            
                
                    <a href="../day05/02.线程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        线程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day05/03.线程注意点.html">
            
                
                    <a href="../day05/03.线程注意点.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        线程的注意点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day05/04.自定义线程.html">
            
                
                    <a href="../day05/04.自定义线程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        自定义线程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day05/05.多线程-共享全局变量.html">
            
                
                    <a href="../day05/05.多线程-共享全局变量.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        多线程-共享全局变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day05/06.多线程-共享全局变量问题.html">
            
                
                    <a href="../day05/06.多线程-共享全局变量问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        多线程-共享全局变量问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day05/07.互斥锁.html">
            
                
                    <a href="../day05/07.互斥锁.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        互斥锁
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day05/08.死锁.html">
            
                
                    <a href="../day05/08.死锁.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        死锁
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day05/09.案例-多任务udp聊天器.html">
            
                
                    <a href="../day05/09.案例-多任务udp聊天器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        案例-多任务版udp聊天器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day05/10.案例-多任务版TCP服务端.html">
            
                
                    <a href="../day05/10.案例-多任务版TCP服务端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        案例-多任务版TCP服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.11" data-path="day05/11.案例-多任务版文件下载.html">
            
                
                    <a href="../day05/11.案例-多任务版文件下载.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.11.</b>
                        
                        案例-多任务版文件下载
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day06/index.html">
            
                
                    <a href="../day06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        多任务-进程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day06/01.进程介绍.html">
            
                
                    <a href="../day06/01.进程介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        进程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day06/02.进程的使用.html">
            
                
                    <a href="../day06/02.进程的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        进程的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day06/03.进程注意点.html">
            
                
                    <a href="../day06/03.进程注意点.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        进程注意点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day06/04.进程间通信-Queue.html">
            
                
                    <a href="../day06/04.进程间通信-Queue.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        进程间通信-Queue
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day06/05.进程池.html">
            
                
                    <a href="../day06/05.进程池.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        进程的创建-进程池
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="day06/06.进程、线程对比.html">
            
                
                    <a href="../day06/06.进程、线程对比.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        进程和线程的对比
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="day06/07.案例-文件夹拷贝器-多任务版.html">
            
                
                    <a href="../day06/07.案例-文件夹拷贝器-多任务版.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        案例-多任务版文件夹拷贝器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day07/index.html">
            
                
                    <a href="../day07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        多任务-协程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day07/01.迭代.html">
            
                
                    <a href="../day07/01.迭代.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        迭代
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="7.2" data-path="day07/02.迭代器.html">
            
                
                    <a href="../day07/02.迭代器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day07/03.生成器.html">
            
                
                    <a href="../day07/03.生成器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day07/04.协程-yield.html">
            
                
                    <a href="../day07/04.协程-yield.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        协程-yield
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day07/05.协程-greenlet.html">
            
                
                    <a href="../day07/05.协程-greenlet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        协程-greenlet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day07/06.协程-gevent.html">
            
                
                    <a href="../day07/06.协程-gevent.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        协程-gevent
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.7" data-path="day07/07.进程、线程、协程对比.html">
            
                
                    <a href="../day07/07.进程、线程、协程对比.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.7.</b>
                        
                        进程、线程、协程区别
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.8" data-path="day07/08.案例-gevent多任务图片下载.html">
            
                
                    <a href="../day07/08.案例-gevent多任务图片下载.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.8.</b>
                        
                        案例-gevent多任务图片下载
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day08/index.html">
            
                
                    <a href="../day08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        正则表达式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day08/01.正则表达式的介绍.html">
            
                
                    <a href="../day08/01.正则表达式的介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        正则表达式的介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day08/02.re模块介绍.html">
            
                
                    <a href="../day08/02.re模块介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        re模块介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day08/03.匹配单个字符.html">
            
                
                    <a href="../day08/03.匹配单个字符.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        匹配单个字符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day08/04.匹配多个字符.html">
            
                
                    <a href="../day08/04.匹配多个字符.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        匹配多个字符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day08/05.匹配开头和结尾.html">
            
                
                    <a href="../day08/05.匹配开头和结尾.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        匹配开头和结尾
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day08/06.匹配分组.html">
            
                
                    <a href="../day08/06.匹配分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        匹配分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="day08/07.re模块的高级用法.html">
            
                
                    <a href="../day08/07.re模块的高级用法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        re模块的高级用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.8" data-path="day08/08.贪婪和非贪婪.html">
            
                
                    <a href="../day08/08.贪婪和非贪婪.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.8.</b>
                        
                        贪婪和非贪婪
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.9" data-path="day08/09.r的作用.html">
            
                
                    <a href="../day08/09.r的作用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.9.</b>
                        
                        r的作用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="day09/index.html">
            
                
                    <a href="../day09/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        HTTP协议
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="day09/01.浏览器访问服务器的过程.html">
            
                
                    <a href="../day09/01.浏览器访问服务器的过程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        浏览器访问服务器的过程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="day09/02.http协议.html">
            
                
                    <a href="../day09/02.http协议.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        HTTP协议介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="day09/03.谷歌浏览器开发者工具查看http协议的通信过程.html">
            
                
                    <a href="../day09/03.谷歌浏览器开发者工具查看http协议的通信过程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        开发工具查看http协议通信过程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="day09/04.http请求报文.html">
            
                
                    <a href="../day09/04.http请求报文.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        HTTP请求报文格式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="day09/05.获取请求报文原数据.html">
            
                
                    <a href="../day09/05.获取请求报文原数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        获取请求报文原数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.6" data-path="day09/06.http响应报文.html">
            
                
                    <a href="../day09/06.http响应报文.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.6.</b>
                        
                        HTTP响应报文格式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.7" data-path="day09/07.tcp长连接和短连接.html">
            
                
                    <a href="../day09/07.tcp长连接和短连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.7.</b>
                        
                        长连接和短连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.8" data-path="day09/08.案例-模拟浏览器的实现.html">
            
                
                    <a href="../day09/08.案例-模拟浏览器的实现.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.8.</b>
                        
                        案例-模拟浏览器的实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="day10/index.html">
            
                
                    <a href="../day10/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Web静态服务器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="day10/01.返回固定数据.html">
            
                
                    <a href="../day10/01.返回固定数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        返回固定数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="day10/02.返回固定页面数据.html">
            
                
                    <a href="../day10/02.返回固定页面数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        返回固定页面数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="day10/03.返回指定页面数据.html">
            
                
                    <a href="../day10/03.返回指定页面数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        返回指定页面数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="day10/04.返回404页面数据.html">
            
                
                    <a href="../day10/04.返回404页面数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        返回404页面数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="day10/05.多任务web服务器.html">
            
                
                    <a href="../day10/05.多任务web服务器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        多任务web服务器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="day10/06.web服务器面向对象开发.html">
            
                
                    <a href="../day10/06.web服务器面向对象开发.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        web服务器面向对象开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="day10/07.给web服务器添加命令行参数.html">
            
                
                    <a href="../day10/07.给web服务器添加命令行参数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        给web服务器添加命令行参数
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8FED;&#x4EE3;&#x5668;">&#x8FED;&#x4EE3;&#x5668;</h1>
<h2 id="&#x76EE;&#x6807;">&#x76EE;&#x6807;</h2>
<ul>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x4F5C;&#x7528;</li>
</ul>
<h3 id="1-&#x81EA;&#x5B9A;&#x4E49;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;">1. &#x81EA;&#x5B9A;&#x4E49;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;</h3>
<blockquote>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;: &#x5728;&#x7C7B;&#x91CC;&#x9762;&#x5B9A;&#x4E49;<code>__iter__</code>&#x548C;<code>__next__</code>&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Iterable
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Iterator

<span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;: &#x5728;&#x7C7B;&#x91CC;&#x9762;&#x5B9A;&#x4E49;__iter__&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyList</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        self.my_list = list()

    <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6307;&#x5B9A;&#x5143;&#x7D20;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">append_item</span><span class="hljs-params">(self, item)</span>:</span>
        self.my_list.append(item)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x672C;&#x8D28;&#xFF1A;&#x904D;&#x5386;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#x5176;&#x5B9E;&#x83B7;&#x53D6;&#x7684;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C; &#x7136;&#x540E;&#x901A;&#x8FC7;&#x8FED;&#x4EE3;&#x5668;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x6570;&#x636E;</span>
        my_iterator = MyIterator(self.my_list)
        <span class="hljs-keyword">return</span> my_iterator


<span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;: &#x5728;&#x7C7B;&#x91CC;&#x9762;&#x5B9A;&#x4E49;__iter__&#x548C;__next__&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyIterator</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, my_list)</span>:</span>
        self.my_list = my_list

        <span class="hljs-comment"># &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x4E0B;&#x6807;</span>
        self.current_index = <span class="hljs-number">0</span>

        <span class="hljs-comment"># &#x5224;&#x65AD;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x8FED;&#x4EE3;&#x5668;</span>
        result = isinstance(self, Iterator)
        print(<span class="hljs-string">&quot;MyIterator&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x8FED;&#x4EE3;&#x5668;:&quot;</span>, result)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self

    <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.current_index &lt; len(self.my_list):
            self.current_index += <span class="hljs-number">1</span>
            <span class="hljs-keyword">return</span> self.my_list[self.current_index - <span class="hljs-number">1</span>]
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x6570;&#x636E;&#x53D6;&#x5B8C;&#x4E86;&#xFF0C;&#x9700;&#x8981;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x505C;&#x6B62;&#x8FED;&#x4EE3;&#x7684;&#x5F02;&#x5E38;</span>
            <span class="hljs-keyword">raise</span> StopIteration


my_list = MyList()
my_list.append_item(<span class="hljs-number">1</span>)
my_list.append_item(<span class="hljs-number">2</span>)
result = isinstance(my_list, Iterable)

print(result)

<span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> my_list:
    print(value)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;:</p>
<pre><code>True
MyIterator&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x8FED;&#x4EE3;&#x5668;: True
1
2
</code></pre><h3 id="2-iter&#x51FD;&#x6570;&#x4E0E;next&#x51FD;&#x6570;">2. iter()&#x51FD;&#x6570;&#x4E0E;next()&#x51FD;&#x6570;</h3>
<ol>
<li>iter&#x51FD;&#x6570;: &#x83B7;&#x53D6;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x8EAB;&#x4E0A;&#x7684;<code>__iter__</code>&#x65B9;&#x6CD5;</li>
<li>next&#x51FD;&#x6570;: &#x83B7;&#x53D6;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x8EAB;&#x4E0A;&#x7684;<code>__next__</code>&#x65B9;&#x6CD5;</li>
</ol>
<pre><code class="lang-python"><span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;: &#x5728;&#x7C7B;&#x91CC;&#x9762;&#x5B9A;&#x4E49;__iter__&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyList</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        self.my_list = list()

    <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6307;&#x5B9A;&#x5143;&#x7D20;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">append_item</span><span class="hljs-params">(self, item)</span>:</span>
        self.my_list.append(item)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x672C;&#x8D28;&#xFF1A;&#x904D;&#x5386;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#x5176;&#x5B9E;&#x83B7;&#x53D6;&#x7684;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C; &#x7136;&#x540E;&#x901A;&#x8FC7;&#x8FED;&#x4EE3;&#x5668;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x6570;&#x636E;</span>
        my_iterator = MyIterator(self.my_list)
        <span class="hljs-keyword">return</span> my_iterator


<span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;: &#x5728;&#x7C7B;&#x91CC;&#x9762;&#x5B9A;&#x4E49;__iter__&#x548C;__next__&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;</span>
<span class="hljs-comment"># &#x8FED;&#x4EE3;&#x5668;&#x662F;&#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x6570;&#x636E;&#x7684;&#x4F4D;&#x7F6E;&#x4EE5;&#x4FBF;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x7684;&#x503C;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyIterator</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, my_list)</span>:</span>
        self.my_list = my_list

        <span class="hljs-comment"># &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x4E0B;&#x6807;</span>
        self.current_index = <span class="hljs-number">0</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self

    <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.current_index &lt; len(self.my_list):
            self.current_index += <span class="hljs-number">1</span>
            <span class="hljs-keyword">return</span> self.my_list[self.current_index - <span class="hljs-number">1</span>]
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x6570;&#x636E;&#x53D6;&#x5B8C;&#x4E86;&#xFF0C;&#x9700;&#x8981;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x505C;&#x6B62;&#x8FED;&#x4EE3;&#x7684;&#x5F02;&#x5E38;</span>
            <span class="hljs-keyword">raise</span> StopIteration

<span class="hljs-comment"># &#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</span>
my_list = MyList()
my_list.append_item(<span class="hljs-number">1</span>)
my_list.append_item(<span class="hljs-number">2</span>)

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x8FED;&#x4EE3;&#x5668;</span>
my_iterator = iter(my_list)
print(my_iterator)
<span class="hljs-comment"># &#x83B7;&#x53D6;&#x8FED;&#x4EE3;&#x5668;&#x4E2D;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;</span>
<span class="hljs-comment"># value = next(my_iterator)</span>
<span class="hljs-comment"># print(value)</span>

<span class="hljs-comment"># &#x5FAA;&#x73AF;&#x901A;&#x8FC7;&#x8FED;&#x4EE3;&#x5668;&#x83B7;&#x53D6;&#x6570;&#x636E;</span>
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
    <span class="hljs-keyword">try</span>:
        value = next(my_iterator)
        print(value)
    <span class="hljs-keyword">except</span> StopIteration <span class="hljs-keyword">as</span> e:
        <span class="hljs-keyword">break</span>
</code></pre>
<h3 id="3-for&#x5FAA;&#x73AF;&#x7684;&#x672C;&#x8D28;">3. for&#x5FAA;&#x73AF;&#x7684;&#x672C;&#x8D28;</h3>
<p>&#x904D;&#x5386;&#x7684;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;</p>
<ul>
<li>for item in Iterable &#x5FAA;&#x73AF;&#x7684;&#x672C;&#x8D28;&#x5C31;&#x662F;&#x5148;&#x901A;&#x8FC7;iter()&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;Iterable&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C;&#x7136;&#x540E;&#x5BF9;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#x4E0D;&#x65AD;&#x8C03;&#x7528;next()&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;&#x5E76;&#x5C06;&#x5176;&#x8D4B;&#x503C;&#x7ED9;item&#xFF0C;&#x5F53;&#x9047;&#x5230;StopIteration&#x7684;&#x5F02;&#x5E38;&#x540E;&#x5FAA;&#x73AF;&#x7ED3;&#x675F;&#x3002;</li>
</ul>
<p>&#x904D;&#x5386;&#x7684;&#x662F;&#x8FED;&#x4EE3;&#x5668;</p>
<ul>
<li>for item in Iterator &#x5FAA;&#x73AF;&#x7684;&#x8FED;&#x4EE3;&#x5668;&#xFF0C;&#x4E0D;&#x65AD;&#x8C03;&#x7528;next()&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;&#x5E76;&#x5C06;&#x5176;&#x8D4B;&#x503C;&#x7ED9;item&#xFF0C;&#x5F53;&#x9047;&#x5230;StopIteration&#x7684;&#x5F02;&#x5E38;&#x540E;&#x5FAA;&#x73AF;&#x7ED3;&#x675F;&#x3002;</li>
</ul>
<h3 id="4-&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;">4. &#x8FED;&#x4EE3;&#x5668;&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;</h3>
<p>&#x6211;&#x4EEC;&#x53D1;&#x73B0;&#x8FED;&#x4EE3;&#x5668;&#x6700;&#x6838;&#x5FC3;&#x7684;&#x529F;&#x80FD;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;next()&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x6765;&#x8FD4;&#x56DE;&#x4E0B;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x503C;&#x3002;&#x5982;&#x679C;&#x6BCF;&#x6B21;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x503C;&#x4E0D;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x5DF2;&#x6709;&#x7684;&#x6570;&#x636E;&#x96C6;&#x5408;&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;&#x7A0B;&#x5E8F;&#x6309;&#x7167;&#x4E00;&#x5B9A;&#x7684;&#x89C4;&#x5F8B;&#x8BA1;&#x7B97;&#x751F;&#x6210;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x4E5F;&#x5C31;&#x610F;&#x5473;&#x7740;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x518D;&#x4F9D;&#x8D56;&#x4E00;&#x4E2A;&#x5DF2;&#x6709;&#x7684;&#x6570;&#x636E;&#x96C6;&#x5408;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E0D;&#x7528;&#x518D;&#x5C06;&#x6240;&#x6709;&#x8981;&#x8FED;&#x4EE3;&#x7684;&#x6570;&#x636E;&#x90FD;&#x4E00;&#x6B21;&#x6027;&#x7F13;&#x5B58;&#x4E0B;&#x6765;&#x4F9B;&#x540E;&#x7EED;&#x4F9D;&#x6B21;&#x8BFB;&#x53D6;&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x8282;&#x7701;&#x5927;&#x91CF;&#x7684;&#x5B58;&#x50A8;&#xFF08;&#x5185;&#x5B58;&#xFF09;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x6BD4;&#x5982;&#xFF0C;&#x6570;&#x5B66;&#x4E2D;&#x6709;&#x4E2A;&#x8457;&#x540D;&#x7684;&#x6590;&#x6CE2;&#x62C9;&#x5951;&#x6570;&#x5217;&#xFF08;Fibonacci&#xFF09;&#xFF0C;&#x6570;&#x5217;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x6570;&#x4E3A;0&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6570;&#x4E3A;1&#xFF0C;&#x5176;&#x540E;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x6570;&#x90FD;&#x53EF;&#x7531;&#x524D;&#x4E24;&#x4E2A;&#x6570;&#x76F8;&#x52A0;&#x5F97;&#x5230;&#xFF1A;</p>
<p>0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x901A;&#x8FC7;for...in...&#x5FAA;&#x73AF;&#x6765;&#x904D;&#x5386;&#x8FED;&#x4EE3;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x4E2D;&#x7684;&#x524D;n&#x4E2A;&#x6570;&#x3002;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x7528;&#x8FED;&#x4EE3;&#x5668;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x90FD;&#x901A;&#x8FC7;&#x6570;&#x5B66;&#x8BA1;&#x7B97;&#x6765;&#x751F;&#x6210;&#x4E0B;&#x4E00;&#x4E2A;&#x6570;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Fibonacci</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, num)</span>:</span>
        <span class="hljs-comment"># num:&#x8868;&#x793A;&#x751F;&#x6210;&#x591A;&#x5C11;fibonacci&#x6570;&#x5B57;</span>
        self.num = num
        <span class="hljs-comment"># &#x8BB0;&#x5F55;fibonacci&#x524D;&#x4E24;&#x4E2A;&#x503C;</span>
        self.a = <span class="hljs-number">0</span>
        self.b = <span class="hljs-number">1</span>
        <span class="hljs-comment"># &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x751F;&#x6210;&#x6570;&#x5B57;&#x7684;&#x7D22;&#x5F15;</span>
        self.current_index = <span class="hljs-number">0</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.current_index &lt; self.num:
            result = self.a
            self.a, self.b = self.b, self.a + self.b
            self.current_index += <span class="hljs-number">1</span>
            <span class="hljs-keyword">return</span> result
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">raise</span> StopIteration


fib = Fibonacci(<span class="hljs-number">5</span>)
<span class="hljs-comment"># value = next(fib)</span>
<span class="hljs-comment"># print(value)</span>

<span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> fib:
    print(value)
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;:</p>
<pre><code class="lang-python"><span class="hljs-number">0</span>
<span class="hljs-number">1</span>
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
</code></pre>
<h3 id="&#x5C0F;&#x7ED3;">&#x5C0F;&#x7ED3;</h3>
<p>&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x662F;&#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x6570;&#x636E;&#x7684;&#x4F4D;&#x7F6E;&#x4EE5;&#x4FBF;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x7684;&#x503C;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day07/01.迭代.html" class="navigation navigation-prev " aria-label="Previous page: 迭代"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day07/03.生成器.html" class="navigation navigation-next " aria-label="Next page: 生成器"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
